﻿using System;
using System.Collections.Generic;
using System.Web.UI;
using System.Web.UI.WebControls;
using SARS.Biz;
using SARS.Entity;

namespace SARS.Student
{
    public partial class ViewAttendanceRecord : Page
    {
        private readonly CourseBO courseBiz = new CourseBO();
        private readonly ProgramBO programBiz = new ProgramBO();
        private readonly SessionBO sessionBiz = new SessionBO();
        private readonly UserBO userBiz = new UserBO();

        private StudentEntity student;
        private int lastCourseId = 0;
        private int lastProgramId = 0;
        protected void Page_Load(object sender, EventArgs e)
        {
            Response.Cache.SetNoStore();
            UserEntity user = userBiz.RetrieveUserByUserName(Page.User.Identity.Name);
            if (user.UserRole != RoleType.Student)
            {
                Response.Write("<script language='javascript'>window.alert('Your role is not allow to access this page.');" +
                               "window.location='../Account/Login.aspx';</script>");

            }
            student = (StudentEntity) user;
            lblName.Text = student.FullName;
            List<ProgramEntity> programs = programBiz.RetrieveProgramByStudentId(student.UserID);

            if (!IsPostBack)
            {
                ucProgramCourse.ProgramDataSource = programs;
            }
        }

        protected void Page_LoadComplete(object sender, EventArgs e)
        {
            //ucProgramCourse.
        }

        protected void ucProgramCourse_OnSelectedCourseChanged()
        {
            if (ucProgramCourse.SelectedCourseValue != 0)
            {
                List<AttendanceSessionEntity> listAttendances =
                    sessionBiz.GetAttendanceSessionByCourseIdAndStudentId(ucProgramCourse.SelectedCourseValue,
                        student.UserID);

                gvStudentAtt.DataSource = listAttendances;
                gvStudentAtt.DataBind();
                lastCourseId = ucProgramCourse.SelectedCourseValue;
            }
        }

        protected void ucProgramCourse_OnSelectedProgramChanged()
        {
            if (ucProgramCourse.SelectedProgramValue != 0)
            {
                List<CourseEntity> listCourse =
                    courseBiz.GetAllCourseByStudentIdAndProgramId(ucProgramCourse.SelectedProgramValue, student.UserID);
                ucProgramCourse.CourseDataSource = listCourse;
                lastProgramId = ucProgramCourse.SelectedProgramValue;
            }
        }

        protected void btNotify_OnClick(object sender, EventArgs e)
        {
            var btnSelect = sender as LinkButton;
            if (btnSelect != null)
            {
                var row = (GridViewRow) btnSelect.NamingContainer;
                string attendanceId = gvStudentAtt.DataKeys[row.RowIndex].Value.ToString();
                string sesid = row.Cells[0].Text;
                string linkurl = string.Format("AbsenceNotify.aspx?sesid={0}&stid={1}&cid={2}&prid={3}&atid={4}"
                    , sesid
                    , student.UserID
                    , lastCourseId
                    , lastProgramId
                    , attendanceId);

                Response.Redirect(linkurl);
            }
        }
    }
}